package com.bckj.fastboot.core.extra.i18n;

import org.springframework.context.MessageSource;
import org.springframework.context.support.AbstractMessageSource;

import java.text.MessageFormat;
import java.util.Locale;

/**
 * FastMessageSource
 *
 * @author wfc
 **/
public class FastMessageSource extends AbstractMessageSource {
    private final I18nMessageRepository repository;

    public FastMessageSource(MessageSource parent, I18nMessageRepository repository) {
        this.repository = repository;
        setParentMessageSource(parent);
    }

    @Override
    protected MessageFormat resolveCode(String code, Locale locale) {
        String message = repository.findMessageByCodeAndLocale(code, locale.toString());
        // 如果找到，返回MessageFormat；若未找到，可以返回null或使用code作为默认值
        if (message != null) {
            return new MessageFormat(message, locale);
        }
        // 返回null，会触发父类的回退逻辑（例如查找默认属性文件）
        return null;
    }
}
